package sort.leetcode;

import java.util.LinkedList;
import java.util.List;

/**
 * 148. 排序链表
 */
public class SortList {
    public static class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) {
            this.val = val;
        }
    }

    public static ListNode sortList(ListNode head) {
        // 分析链表需要保证不丢节点的同时进行排序，所以只能相邻节点比较交换位置
        // 采用归并排序
        if (head == null) {
            return null;
        }
        List<ListNode> list = new LinkedList<>();
        int len = 0;
        while (head != null) {
            len ++;
            list.add(head);
            head = head.next;
        }
        ListNode[] listNodes = new ListNode[len];
        for (int i = 0; i < len; i++) {
            listNodes[i] = list.get(i);
        }
        mergeSortList(listNodes, 0, len - 1);
        head = listNodes[0];
        ListNode tmp = head;
        for (int i = 1; i < len; i++) {
            tmp.next = listNodes[i];
            tmp = tmp.next;
        }
        tmp.next = null;
        return head;
    }

    private static void mergeSortList(ListNode[] listNodes, int left, int right) {
        if (right - left <= 10) {
            insertionSort(listNodes, left, right);
            return;
        }
        int mid = left + ((right - left) >> 1);
        mergeSortList(listNodes, left, mid);
        mergeSortList(listNodes, mid + 1, right);
        if (listNodes[mid].val > listNodes[mid + 1].val) {
            merge(listNodes, left, mid, right);
        }
    }

    private static void merge(ListNode[] listNodes, int left, int mid, int right) {
        ListNode[] copyList = new ListNode[right - left + 1];
        for (int i = left; i <= right; i++) {
            copyList[i - left] = listNodes[i];
        }
        int index1 = left;
        int index2 = mid + 1;
        for (int i = left; i <= right; i++) {
            if (index1 > mid) {
                listNodes[i] = copyList[index2 - left];
                index2 ++;
            } else if (index2 > right) {
                listNodes[i] = copyList[index1 - left];
                index1 ++;
            } else if (copyList[index1 - left].val <= copyList[index2 - left].val) {
                listNodes[i] = copyList[index1 - left];
                index1 ++;
            } else {
                listNodes[i] = copyList[index2 - left];
                index2 ++;
            }
        }
    }

    private static void insertionSort(ListNode[] listNodes, int left, int right) {
        for (int i = left + 1; i <= right; i++) {
            for (int j = i; j > left && listNodes[j].val < listNodes[j - 1].val ; j--) {
                swap(listNodes, j, j - 1);
            }
        }
    }

    private static void swap(ListNode[] listNodes, int j, int i) {
        ListNode tmp = listNodes[j];
        listNodes[j] = listNodes[i];
        listNodes[i] = tmp;
    }

    public static void main(String[] args) {
        int[] nums = new int[] {4, 2, 1, 3};
        ListNode head = new ListNode(nums[0]);
        ListNode tmp = head;
        for (int i = 1; i < nums.length; i++) {
            tmp.next = new ListNode(nums[i]);
            tmp = tmp.next;
        }
        sortList(head);
        System.out.println();
    }
}
